import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import func from '../utils/tools'
import { ElMessage } from 'element-plus'

const routes:Array<RouteRecordRaw> = [
    {
        path: '/',
        name: 'home',
        redirect: '/business/chartList',
    },
    {
        path: '/business',
        name: 'business',
        component: () => import('@/layout/index.vue'),
        children: [
            {
                path: 'EvTestComponent',
                name: 'EvTestComponent',
                component: () => import('@/views/EvTestComponent/index.vue'),
                meta: {
                    appId: 'ed1198f7-828c-4b41-8555-69b1daf7a03e',
                },
            },
            {
                path: 'chartList',
                name: 'chartList',
                component: () => import('@/views/chart/chartList/index.vue'),
                meta: {
                    appId: 'c8848659-b27d-42d7-a66a-2b2fe247df55',
                },
            },
            {
                path: 'chartConfig',
                name: 'chartConfig',
                component: () => import('@/views/chart/chartConfig/index.vue'),
                meta: {
                    appId: 'ced8ef93-cf8f-44cd-9738-19b4b00c4440',
                },
                props: route => ({ 
                    chartType: route.query.chartType || route.params.chartType,
                    opType: route.query.opType || route.params.opType, 
                    chartId: route.query.chartId || route.query.chartId,
                }),
            },
            
        ],
    },
    {
        path: '/sysmanage',
        name: 'sysmanage',
        component: () => import('@/layout/index.vue'),
        children: [{
            path: 'userManage',
            name: 'userManage',
            component: () => import('@/views/sys/userManage/index.vue'),
            meta: {
                appId: '755db490-7d66-4b33-af25-f7e95ce24ad2',
            },
        },
        {
            path: 'menuManage',
            name: 'menuManage',
            component: () => import('@/views/sys/menuManage/index.vue'),
            meta: {
                appId: '320a641e-55e8-4191-acf4-132f4747c0f8',
            },
        },
        {
            path: 'dictManage',
            name: 'dictManage',
            component: () => import('@/views/sys/dictManage/index.vue'),
            meta: {
                appId: '754ab163-fb70-45a9-8365-e16228d90be9',
            },
        },
        {
            path: 'configManage',
            name: 'configManage',
            component: () => import('@/views/sys/configManage/index.vue'),
            meta: {
                appId: '086972fb-23f7-43d8-a9f0-2ffe377829a5',
            },
        },
        {
            path: 'sysLogs',
            name: 'sysLogs',
            component: () => import('@/views/userLogs/index.vue'),
            meta: {
                appId: '186972fb-23f7-43d8-a9f0-2ffe377829a5',
            },
        },
        ],
    },
    
    {
        path: '/outer',
        name: 'outer',
        component: () => import('@/layout/index.vue'),
        children: [{
            path: 'apiDoc',
            name: 'apiDoc',
            component: () => import('@/views/outer/apiDoc.vue'),
            meta: {
                appId: 'ae12e51a-07a5-4d4b-900c-a2393698481c',
            },
        }],
    },
    {
        path: '/view',
        name: 'view',
        component: () => import('@/views/view/index.vue'),
        children: [{
            path: 'chartView/:chartId',
            name: 'chartView',
            component: () => import('@/components/EvChart.vue'),
            props: route => ({
                chartId: route.query.chartId || route.params.chartId,
            }),
        }],
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('@/views/login/index.vue'),
    },
    {
        path: '/register',
        name: 'register',
        component: () => import('@/views/register/index.vue'),
    },
    {
        path: '/404',
        name: '404',
        component: () => import('@/views/404.vue'),
    },
]

const router = createRouter({
    history: createWebHashHistory(),
    routes,
})

router.beforeEach((to, from, next) => {
    let cookieToken = func.getCookie('ev-token')
    if (!cookieToken && to.name !== 'login' && to.name !== 'register') {
        ElMessage({
            message: '您还未登录，请先登录！',
            type: 'error',
        })
        next({
            name: 'login',
        })
    } else next()
})

export default router